<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  header("location:checklogin.php");
}
$branchIdSelected          = "";
$branchIdList              = isset($_REQUEST['branchId']) ? $_REQUEST['branchId'] : 0;
$transactionIdSelected     = isset($_REQUEST['transactionInId']) ? $_REQUEST['transactionInId'] : 0;
$totalCr                   = isset($_REQUEST['totalCr']) ? $_REQUEST['totalCr'] : 0;
$totalDr                   = isset($_REQUEST['totalDr']) ? $_REQUEST['totalDr'] : 0;
$total                     = isset($_REQUEST['total']) ? $_REQUEST['total'] : 0;
$fromIdSession             = $_SESSION['ses_branchName'];
if(isset($_POST['insertBtn']))
{  
	
  $party            = (isset($_POST['party'])) ? $_POST['party'] : 0;
  $driver           = (isset($_POST['driver'])) ? $_POST['driver'] : 0;
  $transactionIn    = (isset($_POST['transactionIn'])) ? $_POST['transactionIn'] : 0;
  $other            = (isset($_POST['other'])) && ($_POST['other'] != "") ? $_POST['other'] : "";
  $moneyTrDate      = $_POST['moneyTrDateYear']."-".$_POST['moneyTrDateMonth']."-".$_POST['moneyTrDateDay'];
  $crDr             = (isset($_POST['crDr'])) && ($_POST['crDr'] != "") ? $_POST['crDr'] : "";
  $amount           = (isset($_POST['amount'])) && ($_POST['amount'] != "") ? $_POST['amount'] : 0;
  $accountId        = (isset($_POST['accountId'])) && ($_POST['accountId'] != "") ? $_POST['accountId'] : 0;
  $narration        = (isset($_POST['narration'])) && ($_POST['narration'] != "") ? $_POST['narration'] : "" ;
  $branchIdSelected = (isset($_POST['branchIdSelected']))&& ($_POST['branchIdSelected'] !='') ? $_POST['branchIdSelected'] : "";
  
  $insertMoney = "INSERT INTO moneytrans (transactionInId, branchId, partyId, driverId,accountId,other,date, crDr, amount, narration)
                  VALUES ('".$transactionIn."','".$branchIdSelected."','".$party."','".$driver."','".$accountId."','".$other."',
                          '".$moneyTrDate."','".$crDr."',".$amount.",'".$narration."')";
  $insertMoneyRes = mysql_query($insertMoney);
  if(!$insertMoneyRes)
  {
  	echo "Insert Error : ".mysql_error();
  }
}

//Select Query of Transaction In
$transactionInArray = array();
$i = 0;
$selectTransaction = "SELECT transactionInId, transactionIn
                        FROM transactionin";
$selectTransactionRes  = mysql_query($selectTransaction);
while($transactionInRow = mysql_fetch_array($selectTransactionRes))
{
  $transactionInArray['transactionInId'][$i] = $transactionInRow['transactionInId'];
  $transactionInArray['transactionIn'][$i]   = $transactionInRow['transactionIn'];
  $i++;
}

//Select Query of Branch In

//Select Query of Transaction In For List : Start
$transactionInListArray = array();
$b = 0;
$selectTransaction = "SELECT transactionInId, transactionIn
                        FROM transactionin
                    ORDER BY transactionIn";
$selectTransactionRes  = mysql_query($selectTransaction);
while($transactionInRow = mysql_fetch_array($selectTransactionRes))
{
  $transactionInListArray['transactionInId'][$b] = $transactionInRow['transactionInId'];
  $transactionInListArray['transactionIn'][$b]   = $transactionInRow['transactionIn'];
  $b++;
}

//Select Query of Transaction In For List : Start


$j=0;
$branchArray = array();
$selectBranch = "SELECT branchId,branchName
                   FROM branch
               ORDER BY branchName";
$selectBranchResult = mysql_query($selectBranch);
while($branchRow = mysql_fetch_array($selectBranchResult))
{
  $branchArray['branchId'][$j]   = $branchRow['branchId'];
  $branchArray['branchName'][$j] = $branchRow['branchName'];
  $j++;
}

//Select Query of Party In
$k=0;
$partyArray = array();
$selectParty = "SELECT partyId,name
                  FROM party
              ORDER BY name";
$selectPartyRes = mysql_query($selectParty);
while($partyRow = mysql_fetch_array($selectPartyRes))
{
	$partyArray['partyId'][$k] = $partyRow['partyId'];
	$partyArray['name'][$k]    = $partyRow['name'];
	$k++;
}

//Select Query of Driver
$driverArray = array();
$x = 0;
$selectDriver = "SELECT driverId, driverName
                   FROM driver
               ORDER BY driverName";
$selectDriverRes  = mysql_query($selectDriver);
while($driverRow = mysql_fetch_array($selectDriverRes))
{
  $driverArray['driverId'][$x]   = $driverRow['driverId'];
  $driverArray['driverName'][$x] = $driverRow['driverName'];
  $x++;
}

//Select Query of Account
$accountArray = array();
$y = 0;
$selectDriver = "SELECT accountId, accountName
                   FROM account
               ORDER BY accountName";
$selectDriverRes  = mysql_query($selectDriver);
while($driverRow = mysql_fetch_array($selectDriverRes))
{
  $accountArray['accountId'][$y]     = $driverRow['accountId'];
  $accountArray['accountName'][$y]   = $driverRow['accountName'];
  $y++;
}

$moneyTransArray = array();
$y = 0;
$selectMoneyTransaction = "SELECT party.name, branch.branchName, driver.driverName, transactionin.transactionIn,moneyTransId,
                                  accountName,branchName,other,DATE_FORMAT(date,'%d-%m-%Y') AS date, crDr, amount, narration
                             FROM moneytrans
                             LEFT JOIN party ON moneytrans.partyId = party.partyId
                             LEFT JOIN branch ON moneytrans.branchId = branch.branchId
                             LEFT JOIN transactionin ON moneytrans.transactionInId = transactionin.transactionInId
                             LEFT JOIN account ON moneytrans.accountId = account.accountId
                             LEFT JOIN driver ON moneytrans.driverId = driver.driverId
                            WHERE 1 = 1";                                 
if(isset($_REQUEST['branchId']) && $_REQUEST['branchId'] != 0)
{
  $selectMoneyTransaction .= " AND branch.branchId = ".$_REQUEST['branchId'];
}

if(isset($_REQUEST['transactionInId']) && $_REQUEST['transactionInId'] != 0)
{
  $selectMoneyTransaction .= " AND moneytrans.transactionInId = ".$_REQUEST['transactionInId'];
}
$result = mysql_query($selectMoneyTransaction);
while($moneyRow = mysql_fetch_array($result))
{
  $moneyTransArray[$y]['moneyTransId']  = $moneyRow['moneyTransId'];
  $moneyTransArray[$y]['branchName']    = $moneyRow['branchName'];
  $moneyTransArray[$y]['name']          = $moneyRow['name'];
  $moneyTransArray[$y]['driverName']    = $moneyRow['driverName'];
  $moneyTransArray[$y]['transactionIn'] = $moneyRow['transactionIn'];
  $moneyTransArray[$y]['other']         = $moneyRow['other'];
  $moneyTransArray[$y]['date']          = $moneyRow['date'];
  $moneyTransArray[$y]['crDr']          = $moneyRow['crDr'];
  $moneyTransArray[$y]['amount']        = $moneyRow['amount'];
  $moneyTransArray[$y]['accountName']   = $moneyRow['accountName'];
  $moneyTransArray[$y]['narration']     = $moneyRow['narration'];
  if($moneyRow['crDr'] == 'Cr')
  {
    $totalCr                              += $moneyRow['amount'];
  }
  if($moneyRow['crDr'] == 'Dr')
  {
    $totalDr                              += $moneyRow['amount'];
  }
  $total = $totalCr - $totalDr;
  $y++;
}

include("./bottom.php");
$smarty->assign("totalCr",$totalCr);
$smarty->assign("totalDr",$totalDr);
$smarty->assign("transactionInArray",$transactionInArray);
$smarty->assign('branchIdSelected',$_SESSION['ses_branchId']);
$smarty->assign('branchNameSelected',$_SESSION['ses_branchName']);
$smarty->assign("branchArray",$branchArray);
$smarty->assign("partyArray",$partyArray);
$smarty->assign("driverArray",$driverArray);
$smarty->assign("accountArray",$accountArray);
$smarty->assign("moneyTransArray",$moneyTransArray);
$smarty->assign("branchIdList",$branchIdList);
$smarty->assign("transactionInListArray",$transactionInListArray);
$smarty->assign("transactionIdSelected",$transactionIdSelected);
$smarty->assign("total",$total);
$smarty->display('moneyTransaction.tpl');
?>